import type {
  SaveOrUpdateIndicatorParams,
  VirtuallyRelationSaveOrUpdate,
} from '@/types'
import { pointApi } from '@/api'

export const defaultFormData: Partial<SaveOrUpdateIndicatorParams>
  = {
    pointId: 0,
    indicatorCode: '',
    indicatorName: '',
    parentId: null as any,
    indicatorType: '',
    statisticalType: '1',
    color: '',
    shape: '',
    unit: '',
    indicatorNumber: '',
    // isVirtually: 0,
    indicatorMultiple: 1,
    energyId: null as any,
    source: 0,
  }

export const defaultVirtuallyFormData: Partial<VirtuallyRelationSaveOrUpdate>
  = {
    indicatorId: null as any,
    calculation: null as any,
  }
export const virtuallyFormRules = generateCurrencyFormRules(
  ['indicatorId', 'calculation'],
)
export const calculationObj: Record<number, any> = {
  1: {
    label: '加',
    value: '+',
    class: 'text-blue',
  },
  2: {
    label: '减',
    value: '-',
    class: 'text-red',
  },
}
const rand = () => (Math.random() * 2000 + 1500).toFixed(2)

/** 地下水 */
const color1 = '#06a3a3'
/** 疏肝水 */
const color2 = '#ff7f00'
/** 生活污水 */
const color3 = '#3e3eb3'
/** 生活复用水 */
const color4 = '#36bd36'

export const indicatorsGraphs = [
  [
    {
      id: 1,
      parentId: 0,
      pointName: '清水泵房',
      indicatorName: '指标名称#1',
      type: '地下水',
      yesterday: rand(),
      today: rand(),
      unit: 't',
      color: color1,
      parentColor: '',
      shape: null,
      children: [
        {
          id: 2,
          parentId: 1,
          pointName: '矿区生活',
          indicatorName: '指标名称#2',
          type: '地下水',
          yesterday: rand(),
          today: rand(),
          unit: 't',
          color: color1,
          parentColor: color1,
          shape: null,
          children: [
            {
              id: 4,
              parentId: 2,
              pointName: '南泵房蓄水池',
              indicatorName: '指标名称#4',
              type: '生活污水',
              yesterday: rand(),
              today: rand(),
              unit: 't',
              color: color3,
              parentColor: color1,
              shape: null,
            },
            {
              id: 5,
              parentId: 2,
              pointName: '生活污水处理车间',
              indicatorName: '指标名称#5',
              type: '生活复用水',
              yesterday: rand(),
              today: rand(),
              unit: 't',
              color: color4,
              parentColor: color1,
              shape: null,
            },
          ],
        },
        {
          id: 3,
          parentId: 1,
          pointName: '高位水池',
          indicatorName: '指标名称#3',
          type: '地下水',
          yesterday: rand(),
          today: rand(),
          unit: 't',
          color: color1,
          parentColor: color1,
          shape: null,
          children: [
            {
              id: 6,
              parentId: 3,
              pointName: '矿井水池',
              indicatorName: '指标名称#6',
              type: '疏肝水',
              yesterday: rand(),
              today: rand(),
              unit: 't',
              color: color2,
              parentColor: color1,
              shape: null,
              children: [
                {
                  id: 7,
                  parentId: 6,
                  pointName: '矿水井处理厂',
                  indicatorName: '指标名称#7',
                  type: '疏肝水',
                  yesterday: rand(),
                  today: rand(),
                  unit: 't',
                  color: color2,
                  parentColor: color2,
                  shape: null,
                  children: [
                    {
                      id: 8,
                      parentId: 7,
                      pointName: '水池A',
                      indicatorName: '指标名称#8',
                      type: '生活复用水',
                      yesterday: rand(),
                      today: rand(),
                      unit: 't',
                      color: color4,
                      parentColor: color2,
                      shape: null,
                      children: [
                        {
                          id: 10,
                          parentId: 8,
                          pointName: '井下生产',
                          indicatorName: '指标名称#10',
                          type: '地下水',
                          yesterday: rand(),
                          today: rand(),
                          unit: 't',
                          color: color1,
                          parentColor: color4,
                          shape: null,
                        },
                        {
                          id: 11,
                          parentId: 8,
                          pointName: '锅炉房',
                          indicatorName: '指标名称#11',
                          type: '生活污水',
                          yesterday: rand(),
                          today: rand(),
                          unit: 't',
                          color: color3,
                          parentColor: color4,
                          shape: null,
                        },
                      ],
                    },
                    {
                      id: 9,
                      parentId: 7,
                      pointName: '水池B',
                      indicatorName: '指标名称#9',
                      type: '生活复用水',
                      yesterday: rand(),
                      today: rand(),
                      unit: 't',
                      color: color4,
                      parentColor: color2,
                      shape: null,
                      children: [
                        {
                          id: 12,
                          parentId: 9,
                          pointName: '广场绿化',
                          indicatorName: '指标名称#12',
                          type: '生活复用水',
                          yesterday: rand(),
                          today: rand(),
                          unit: 't',
                          color: color4,
                          parentColor: color4,
                          shape: null,
                        },
                      ],
                    },
                  ],
                },
              ],
            },
          ],
        },
        {
          id: 12,
          parentId: 1,
          pointName: '机修厂',
          indicatorName: '指标名称#13',
          type: '地下水',
          yesterday: rand(),
          today: rand(),
          unit: 't',
          color: color1,
          parentColor: color1,
          shape: null,
          children: [
            {
              id: 13,
              parentId: 12,
              pointName: '污水厂',
              indicatorName: '指标名称#14',
              type: '生活污水',
              yesterday: rand(),
              today: rand(),
              unit: 't',
              color: color3,
              parentColor: color1,
              shape: null,
            },
            {
              id: 16,
              parentId: 12,
              pointName: '污水处理池',
              indicatorName: '指标名称#17',
              type: '生活复用水',
              yesterday: rand(),
              today: rand(),
              unit: 't',
              color: color4,
              parentColor: color1,
              shape: null,
            },
          ],
        },
        {
          id: 14,
          parentId: 1,
          pointName: '食堂',
          indicatorName: '指标名称#15',
          type: '地下水',
          yesterday: rand(),
          today: rand(),
          unit: 't',
          color: color1,
          parentColor: color1,
          shape: null,
          children: [
            {
              id: 15,
              parentId: 14,
              pointName: '污水厂',
              indicatorName: '指标名称#16',
              type: '生活污水',
              yesterday: rand(),
              today: rand(),
              unit: 't',
              color: color3,
              parentColor: color1,
              shape: null,
            },
          ],
        },
      ],
    },
  ],
]
export const colors = [
  {
    value: '#E63415',
    label: 'red',
  },
  {
    value: '#FF6600',
    label: 'orange',
  },
  {
    value: '#FFDE0A',
    label: 'yellow',
  },
  {
    value: '#1EC79D',
    label: 'green',
  },
  {
    value: '#14CCCC',
    label: 'cyan',
  },
  {
    value: '#4167F0',
    label: 'blue',
  },
  {
    value: '#6222C9',
    label: 'purple',
  },
]

export const shapes = [
  {
    value: '1',
    label: '矩形',
    type: 'rect',
    class: 'shape rect',
  },
  {
    value: '2',
    label: '圆角矩形',
    type: 'rect-with-round',
    class: 'shape rounded-rect',
  },
  {
    value: '3',
    label: '椭圆',
    type: 'ellipse',
    class: 'shape elliptic',
  },
]

/** 点位树相关 */
export function usePointTree() {
  const treeData = shallowRef<any[]>([])
  const getTreeData = async () => {
    try {
      const { data } = await pointApi.loadTreePoint()
      treeData.value = data || []
    }
    catch (e) {}
  }

  return {
    treeData,

    getTreeData,
  }
}

/** 指标表格相关 */
export * from './useIndicatorTable'

/** 指标分配应用模型相关 */
export * from './useAppModalListeners'
